
import Vue from 'vue';
import Store from '../store';
import { Message } from 'element-ui';
import axios from 'axios'
// let apiUrl = "http://39.100.62.29:9922";
// let apiUrl = 'http://xuehai.shenxunkj.cn';
let apiUrl = 'http://www.hnsjsxx.com';
Vue.prototype.$apiUrl = apiUrl;
// import Qs from "qs";

const instance = axios.create({
    baseURL: apiUrl + '/api',
    //解决跨域
    crossDomain: true,
    timeout: 30000,
    //转换res为json
    // responseType: 'json',
});

// request拦截器
instance.interceptors.request.use(
    config => {
        // 每次发送请求之前检测都vuex存有token,那么都要放在请求头发送给服务器
         
        // let iAdminID = localStorage.getItem('iAdminID')
        // let Token = store.getters['login/userToken']
        // if(Token){
        //      config.headers.Token = Token
        // }else if(iAdminID ){ 
        //     config.headers.Token = JSON.parse(iAdminID).Token
        // }
		// config.Cookie="PHPSESSID=pvf5mnk98t35hvsq1o9c3c92h0" 
        return config
    },
    err => {
        return Promise.reject(err)
    }
)
instance.interceptors.response.use(
    response => {  //成功请求到数据
        // //这里根据后端提供的数据进行对应的处理
		console.log(response)
		
        if (response.data.code == 200) {
            return response.data;
        } else{
			Message({
				type: "warn",
				message: response.data.msg,
				showClose: true
			});
			return Promise.reject(response)
		}

    },
    error => {  //响应错误处理
        console.log(error);
        Message({
            type: "warn",
            message: '网络异常，请重试',
            showClose: true
        });

        return Promise.reject(error)
    }
);


Vue.prototype.$getApi = function(url, data) {
	let userInfoCS = {};
	if(Store.state.userInfo && Store.state.userInfo.token){
		userInfoCS = Store.state.userInfo
	}else{
		userInfoCS = localStorage.getItem("userInfoCS") ? JSON.parse(localStorage.getItem("userInfoCS")) : {};
	}
	if(!data.token){
		data.token = userInfoCS.token || ""
	}
	if(!data.type){
		data.type = userInfoCS.type
	}
	return instance.get(url,{
		params:data
	})
}
Vue.prototype.$postApi = function(url, data) {
	let userInfoCS = {};
	if(Store.state.userInfo && Store.state.userInfo.token){
		userInfoCS = Store.state.userInfo
	}else{
		userInfoCS = localStorage.getItem("userInfoCS") ? JSON.parse(localStorage.getItem("userInfoCS")) : {};
	}
	if(!data.token){
		data.token = userInfoCS.token
	}
	if(!data.type){
		data.type = userInfoCS.type
	}
	return instance.post(url,data)
}

Vue.prototype.$store = Store;

const getDate = (myDate,type,timec) => {
	if(!myDate){
		myDate = new Date()
	}else{
		if(timec == "c"){
			myDate = new Date(parseInt(myDate) * 1000)
		}else{
			myDate = new Date(myDate)
		}
	}
	let year = myDate.getFullYear();   //获取系统的年；
	let month = myDate.getMonth()+1;   //获取系统月份，由于月份是从0开始计算，所以要加1
	
	let date = myDate.getDate(); // 获取系统日，
	
	let hours = myDate.getHours(); //获取系统时，
	let min = myDate.getMinutes(); //分
	let sec = myDate.getSeconds(); //秒
	if(month<10){ month = "0"+month}
	if(date<10){ date = "0"+date }
	if(hours<10){ hours = "0"+hours}
	if(min<10){ min = "0"+min}
	if(sec<10){ sec = "0"+sec }
	
	if(type == '月-号'){
		return month+'月'+date+'号'
	}else
	if(type == '年-月-日'){
		return year+'年'+month+'月'+date+'日'
	}else
	if(type == 's-s-s'){
		return year+'-'+month+'-'+date
	}else
	if(type == 's:s:s'){
		return hours+':'+min+':'+sec
	}else
	if(type == 's-s-s s:s:s'){
		return year+'-'+month+'-'+date+" "+hours+':'+min+':'+sec
	}else{
		return year+'-'+month+'-'+date+" "+hours+':'+min+':'+sec
	}
}
Vue.prototype.$getDate = getDate;
const lastDate = (myDate,type) => {
	var date = new Date();
	var now = date.getTime();
	var str= myDate;
	var endDate = new Date(str);
	var end = endDate.getTime();
	var leftTime = end-now; 
	var d,h,m,s;
	d = Math.floor(leftTime/1000/60/60/24);
	h = Math.floor(leftTime/1000/60/60%24);  
	m = Math.floor(leftTime/1000/60%60);  
	s = Math.floor(leftTime/1000%60);
	if(d<10){ d = "0"+d}
	if(h<10){ h = "0"+h}
	if(m<10){ m = "0"+m}
	if(s<10){ s = "0"+s}
	if (leftTime>=0) {  
		if(type == 'd-s:s:s'){
			return d+"天" +h+":"+m+":"+s
		}else{
			return h+":"+m+":"+s
		}
	} else{
		return "00:00:00"
	}
}
Vue.prototype.$lastDate = lastDate;
const HaveFen= (myDate) => {
	console.log(myDate)
	let dateDay = getDate(new Date(),"s-s-s")
	// let date = getDate(new Date(),"s:s:s")
	let chaTime = new Date(dateDay+" "+myDate+":00").getTime() - new Date().getTime();
	console.log(chaTime)
	if(chaTime > 0){
		var leave1=chaTime%(24*3600*1000)
		var hours=Math.floor(leave1/(3600*1000))
		console.log(hours)
		var leave2=leave1%(3600*1000)    //计算小时数后剩余的毫秒数 
		var minutes=Math.floor(leave2/(60*1000))
		minutes = minutes < 10?"0"+minutes:minutes;
		console.log(minutes)
		return hours+":"+ minutes
	}else{
		return "00:00"
	}
	
}
Vue.prototype.$haveFen = HaveFen;

